/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.momarketing.entity.OnlineSignupConfig;
import com.boxin.ims.modules.momarketing.service.OnlineSignupConfigService;

/**
 * 在线预约Controller
 * @author Jakemanse
 * @version 2013-09-10
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/momarketing/onlineSignupConfig")
public class OnlineSignupConfigController extends BaseController {

	@Autowired
	private OnlineSignupConfigService onlineSignupConfigService;
	
	@ModelAttribute
	public OnlineSignupConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return onlineSignupConfigService.get(id);
		}else{
			return new OnlineSignupConfig();
		}
	}
	
	@RequiresPermissions("momarketing:onlineSignupConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(OnlineSignupConfig onlineSignupConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			onlineSignupConfig.setUser(user);
		}
        Page<OnlineSignupConfig> page = onlineSignupConfigService.find(new Page<OnlineSignupConfig>(request, response), onlineSignupConfig); 
        model.addAttribute("page", page);
		return "modules/momarketing/onlineSignupConfigList";
	}

	@RequiresPermissions("momarketing:onlineSignupConfig:view")
	@RequestMapping(value = "form")
	public String form(OnlineSignupConfig onlineSignupConfig, Model model) {
		User user = UserUtils.getUser();
		OnlineSignupConfig config = onlineSignupConfigService.getByUserId(user.getId());
		if(config != null){
			onlineSignupConfig = config ;
		}else{
			onlineSignupConfig.setUser(user);
		}
		
		
		model.addAttribute("onlineSignupConfig", onlineSignupConfig);
		return "modules/momarketing/onlineSignupConfigForm";
	}

	@RequiresPermissions("momarketing:onlineSignupConfig:edit")
	@RequestMapping(value = "save")
	public String save(OnlineSignupConfig onlineSignupConfig, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, onlineSignupConfig)){
			return form(onlineSignupConfig, model);
		}
		onlineSignupConfigService.save(onlineSignupConfig);
		addMessage(redirectAttributes, "保存配置成功!");
		if(UserUtils.getUser().isAdmin()){
			return "redirect:"+Global.ADMIN_PATH+"/momarketing/onlineSignupConfig/?repage";
		}else{
			return "redirect:"+Global.ADMIN_PATH+"/momarketing/onlineSignupConfig/form";
		}
	}
	
	@RequiresPermissions("momarketing:onlineSignupConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		onlineSignupConfigService.delete(id);
		addMessage(redirectAttributes, "删除成功！");
		if(UserUtils.getUser().isAdmin()){
			return "redirect:"+Global.ADMIN_PATH+"/momarketing/onlineSignupConfig/?repage";
		}else{
			return "redirect:"+Global.ADMIN_PATH+"/momarketing/onlineSignupConfig/form";
		}
	}

}
